package com.eight.course.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eight.common.pojo.EduCourse;
import com.eight.common.pojo.EduSubject;
import com.eight.common.vo.Result;
import com.eight.course.dao.SubjectDao;
import com.eight.course.service.SubjectService;
import com.eight.course.vo.SubjectVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: qy8-education
 * @description:
 * @author: 张亚祥
 * @create: 2023-05-26 19:49
 **/
@Service
public class SubjectServiceImpl implements SubjectService {
    @Resource
    private SubjectDao subjectDao;

    @Override
    public Result findSubjectPage(Integer count, Integer pageSize, SubjectVo subjectVo) {
        Page<EduSubject> page = new Page<>(count, pageSize);
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(subjectVo.getTitle())) {
            wrapper.like("title", subjectVo.getTitle());
        }
        if (subjectVo.getStatusId() != null) {
            wrapper.eq("status_id", subjectVo.getStatusId());
        }
        wrapper.eq("parent_id", "0");
        Page<EduSubject> page1 = subjectDao.selectPage(page, wrapper);
        List<EduSubject> records = page1.getRecords();
        List<EduSubject> list = subjectDao.selectList(null);

            for (EduSubject item : records) {
                List<EduSubject> chiledren = findChildren(item, list);
                item.setChildren(chiledren);
            }
        return Result.success(page1);
    }

    @Override
    public int updateStatus(EduSubject eduSubject) {
        int byId = subjectDao.updateById(eduSubject);
        return byId;
    }

    @Override
    public int insertSubject(EduSubject eduSubject) {
        EduSubject eduSubject1;
            if(StringUtils.hasText(eduSubject.getId())){
            eduSubject1 =new EduSubject(IdUtil.getSnowflakeNextIdStr(),eduSubject.getTitle(), eduSubject.getId(),eduSubject.getStatusId(), eduSubject.getRemark(), eduSubject.getSort(),LocalDateTime.now(),LocalDateTime.now(),new ArrayList<>());
            }else {
                eduSubject1 =  new EduSubject(IdUtil.getSnowflakeNextIdStr(),eduSubject.getTitle(), "0",eduSubject.getStatusId(), eduSubject.getRemark(), eduSubject.getSort(),LocalDateTime.now(),LocalDateTime.now(),new ArrayList<>());
            }
        int insert = subjectDao.insert(eduSubject1);
        return insert;
    }

    @Override
    public int deldeteById(EduSubject eduSubject) {
        int delete = subjectDao.deleteById(eduSubject);
        return delete;
    }


    private List<EduSubject> findChildren(EduSubject item,List<EduSubject> childrens){
        ArrayList<EduSubject> list = new ArrayList<>();
        for (EduSubject children : childrens) {
            if(item.getId().equals(children.getParentId())){
                list.add(children);
            }
        }
        for (EduSubject eduSubject : list) {
            eduSubject.setChildren(findChildren(eduSubject,childrens));
        }
        return list;
    }
}
